package org.csu.myPetStore.web.servlet.Change;

import org.csu.myPetStore.domain.Account;
import org.csu.myPetStore.service.LogcatService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;

public class ViewOrderFormServlet extends HttpServlet {

    private static final String VIEW_ORDER_FORM = "WEB-INF/jsp/order/NewOrderForm.jsp";
    private static final String LOGIN = "/WEB-INF/jsp/account/SignonForm.jsp";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();
        Account account = (Account) session.getAttribute("account");

        LogcatService logcatService = new LogcatService();

        if(account != null) {
            //添加日志信息
            java.util.Date date = new java.util.Date();
            Date datesql = new Date(date.getTime());
            logcatService.InsertLogCat(account.getUsername(),"View Order" + "from cast at " + datesql.toString());

            req.getRequestDispatcher(VIEW_ORDER_FORM).forward(req, resp);
        }
        else {
            session.setAttribute("msg","Please Login In First");
            req.getRequestDispatcher(LOGIN).forward(req, resp);
        }
    }
}
